package com.huirui.server.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huirui.server.mapper.StockShortageRecordMapper;
import com.huirui.server.pojo.dto.StockShortageRecordDto;
import com.huirui.server.pojo.entity.StockShortageRecord;
import com.huirui.server.pojo.result.PageResult;
import com.huirui.server.pojo.vo.StockShortageRecordVo;
import com.huirui.server.service.StockShortageRecordService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;

/**
 * @author yujun
 * @description 针对表【t_stock_shortage_record(缺货登记信息表)】的数据库操作Service实现
 * @createDate 2025-11-23 18:13:00
 */
@Service
public class StockShortageRecordServiceImpl extends ServiceImpl<StockShortageRecordMapper, StockShortageRecord>
        implements StockShortageRecordService {

    @Override
    public void addNewShortageRecordApplication(StockShortageRecordDto stockShortageRecordDto) {
        StockShortageRecord stockShortageRecord = new StockShortageRecord();
        stockShortageRecord.setBrandName(stockShortageRecordDto.getBrandName());
        stockShortageRecord.setProductName(stockShortageRecordDto.getProductName());
        stockShortageRecord.setStatus(0);
        stockShortageRecord.setCreateTime(LocalDateTime.now());
        stockShortageRecord.setProductSpecificationName(stockShortageRecordDto.getProductSpecificationName());
        stockShortageRecord.setProductImages(StringUtils.join(stockShortageRecordDto.getProductImages(), ","));
        save(stockShortageRecord);
    }

    @Override
    public PageResult<StockShortageRecordVo> listStockShortageApplication(String productName, String brandName, Integer status, Long page, Long pageSize) {
        IPage<StockShortageRecord> iPage = new Page<>(page, pageSize);
        lambdaQuery()
                .like(StringUtils.isNotBlank(productName), StockShortageRecord::getProductName, productName)
                .like(StringUtils.isNotBlank(brandName), StockShortageRecord::getBrandName, brandName)
                .eq(status != null && status != -1, StockShortageRecord::getStatus, status)
                .orderByDesc(StockShortageRecord::getCreateTime)
                .page(iPage);
        List<StockShortageRecord> records = iPage.getRecords();
        PageResult<StockShortageRecordVo> pageResult = new PageResult<>();
        pageResult.setSize(iPage.getSize());
        pageResult.setCurrent(iPage.getCurrent());
        pageResult.setTotal(iPage.getTotal());
        pageResult.setPages(iPage.getPages());
        pageResult.setRecords(
                records
                        .stream()
                        .map(shortageRecord -> {
                            StockShortageRecordVo bean = BeanUtil
                                    .copyProperties(shortageRecord, StockShortageRecordVo.class, "productImages");
                            bean.setProductImages(Arrays.asList(shortageRecord.getProductImages().split(",")));
                            return bean;
                        })
                        .toList()
        );
        return pageResult;
    }

    @Override
    public void handleDeleteApplication(Long id) {
        removeById(id);
    }

    @Override
    public void updateStockShortageRecord(StockShortageRecordDto stockShortageRecordDto) {
        lambdaUpdate()
                .eq(StockShortageRecord::getId, stockShortageRecordDto.getId())
                .set(StockShortageRecord::getStatus, stockShortageRecordDto.getStatus())
                .set(StockShortageRecord::getProductName, stockShortageRecordDto.getProductName())
                .set(StockShortageRecord::getBrandName, stockShortageRecordDto.getBrandName())
                .set(StockShortageRecord::getProductSpecificationName, stockShortageRecordDto.getProductSpecificationName())
                .update();
    }
}




